﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Reflection;

namespace ThreeByte.ActiveDeck.Data.UserResponse
{
    public class UserResponseData
    {
        protected static readonly XNamespace ns = Service.ServiceConfiguration.NAMESPACE;
       
        public string Presentation {
            get;
            private set;
        }

        public int Section {
            get;
            protected set;
        }

        public List<AggregateUserData> UserDataElements{
            get; private set;
        }

        public UserResponseData(string presentation, int section) {
            Presentation = presentation;
            Section = section;
            UserDataElements = new List<AggregateUserData>();
        }

        public XElement ToXml() {

            XElement node = new XElement(ns + "UserData", new XAttribute("Presentation", Presentation),
                                                            new XAttribute("Section", Section));

            foreach(AggregateUserData u in UserDataElements) {
                node.Add(u.ToXml());
            }
            return node;
        }

        public static UserResponseData ParseXml(XElement node) {

            UserResponseData userResponseData = new UserResponseData(node.Attribute("Presentation").Value, int.Parse(node.Attribute("Section").Value));
            foreach(XElement a in node.Elements(ns + "Aggregate")) {
                userResponseData.UserDataElements.Add(AggregateUserData.ParseXml(a));
            }

            return userResponseData;
        }

    }
}
